<template>
	<view class="content">
		<cu-custom bgColor="bg-gradual-blue" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">修改部门</block>
		</cu-custom>

		<form id="formEmplotee" @submit="formSubmit">
			<view v-show="false" class="cu-form-group">
				<view class="title">Id</view>
				<input v-model="id" placeholder="请输入部门id" name="id"></input>
			</view>
			<view class="cu-form-group margin-top">
				<view class="title">名称</view>
				<input v-model="departname" placeholder="请输入部门名称" name="departname"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">电话</view>
				<input v-model="mobile" placeholder="请输入电话" maxlength="11" name="mobile"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">地址</view>
				<input v-model="address" placeholder="请输入地址" name="address"></input>
			</view>
			<view class="cu-form-group align-start margin-top">
				<view class="response">
					<view class="title">部门描述</view>
					<textarea v-model="description" name="description" maxlength="-1" placeholder="请输入部门描述"></textarea>
				</view>
			</view>
			
			<button @tap="showDeleteModel" type="" class="cu-bar center bg-white margin-top">
				<view class="action">
					删除部门
				</view>
			</button>
			<view class="bottom response margin-top">
				<button style="background-color: #5599FF;" type="primary" form-type="submit">保存</button>
			</view>
		</form>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: '未获取到上级部门，请返回上级重试',
				id:'',
				departname:'',
				mobile:'',
				address:'',
				description:'',
			}
		},
		onLoad(options) {
			console.log(options)
			if (typeof(options.depart) != 'undefined') {
				let depart = JSON.parse(options.depart)
				this.departname = depart.departname
				this.id = depart.id
				this.mobile = depart.mobile
				this.address = depart.address
				this.description = depart.description
			}
		},
		methods: {
			formSubmit(e) {
				let form = e.detail.value
				console.log(form)
				if (form.departname != "") {
					this.goAdd(form)
				} else {
					uni.showToast({
						title: '请输入部门名',
						icon: 'none'
					})
				}
			},
			showDeleteModel(){
				let that = this
				uni.showModal({
					title:'提示',
					content:'确定要删除该部门吗?',
					success: (res) => {
						if(res.confirm){
							that.deleteDepart()
						}
					}
				})
			},
			deleteDepart(){
				//说明已有数据，目前处于上拉加载
				console.log(this.$constant.DELETE_DEPARTMENT)
				uni.request({
					url: this.$constant.BASEURL + this.$constant.DELETE_DEPARTMENT,
					method: "POST",
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					data: {
						id:this.id
					},
					success: (res) => {
						console.log(res.data)
						if (res.data.code == 0) {
							uni.showToast({
								title: '删除成功',
								icon: 'success'
							})
							let pages = getCurrentPages()
							uni.navigateBack({
								delta: pages.length - 2
							})
						} else {
							uni.showToast({
								title: res.data.message,
								icon: 'none'
							})
						}
					}
				});
			},
			goAdd(form) {
				//说明已有数据，目前处于上拉加载
				uni.request({
					url: this.$constant.BASEURL + this.$constant.ADD_DEPARTMENT,
					method: "POST",
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					data: {
						id:form.id,
						departname: form.departname,
						description: form.description,
						mobile: form.mobile,
						address: form.address
					},
					success: (res) => {
						console.log(res.data)
						if (res.data.code == 0) {
							uni.showToast({
								title: '修改成功',
								icon: 'success'
							})
							let pages = getCurrentPages()
							uni.navigateBack({
								delta: pages.length - 2
							})
						} else {
							uni.showToast({
								title: res.data.message,
								icon: 'none'
							})
						}
					}
				});

			}
		}
	}
</script>

<style>

</style>
